package com.mofeng.myshop.controller.portal;


import com.mofeng.myshop.domain.dto.base.SearchBaseMp;
import com.mofeng.myshop.service.ImageService;
import com.mofeng.myshop.until.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 图片相关 前端控制器
 * </p>
 *
 * @author MoFeng
 * @since 2024-04-17
 */
@Slf4j
@RestController
@RequestMapping("/portal/image")
@RequiredArgsConstructor()
@Tag(name = "图片相关接口", description = "图片相关接口")
public class ImagesController {

    private final ImageService imageService;

    @Operation(summary = "上传图片")
    @PostMapping("")
    public R addImage(@RequestParam("file") MultipartFile image) {
        return imageService.addImage(image);
    }

    @Operation(summary = "获取图片")
    @GetMapping("/{imageId}")
    public void getImage(@PathVariable("imageId") String imageId) throws IOException {
        imageService.getImage(imageId);
    }

    @Operation(summary = "删除图片")
    @DeleteMapping("/{imageId}")
    public R delImage(@PathVariable("imageId") String imageId){
        return imageService.delImage(imageId);
    }

    @GetMapping("/qr-code/{code}")
    public void getQrCodeImage(@PathVariable("code") String code) throws IOException {
        imageService.getQrCodeImage(code);
    }

    /**
     * 查找图片列表
     */
    @GetMapping("/list/{page}/{size}/{original}")
    public R imageList(@PathVariable("page") int page,
                       @PathVariable("size") int size,
                       @PathVariable(name = "original", required = false) String original) {
        SearchBaseMp searchMp = new SearchBaseMp();
        searchMp.setPage(page);
        searchMp.setSize(size);
        searchMp.setKeyword(original);
        return imageService.imageList(searchMp);
    }
}
